package com.qire.manhua.controller;

import android.view.View;

import com.kyleduo.switchbutton.SwitchButton;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.support.base.EventController;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.manhua.activity.MessageSetupActivity;
import com.qire.manhua.dal.service.MessageSetupService;
import com.qire.manhua.databinding.ActivityMessageSetupBinding;

import androidx.annotation.NonNull;

public class MessageSetupController extends EventController<MessageSetupActivity> {

    private MessageSetupService setupService = new MessageSetupService();
    private ProxyViewModel viewModel;

    public MessageSetupController(@NonNull MessageSetupActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityMessageSetupBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setMessageSetupController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ProxyViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void backBtnOnClick(View view) {
        owner.onBackPressed();
    }

    /**
     * 消息推送状态改变事件,监听选择状态会导致控件状态改变时就出发事件，可能会因为其他控件修改引起连锁反应。故改成单击事件处理
     */
    public void messagePushBtnOnClick(View view) {
        SwitchButton switchButton = ObjectUtil.as(view, SwitchButton.class);
        if(ObjectUtil.nonNull(switchButton)) {
            DeviceConfigInfo.updateMessagePushEnable(switchButton.isChecked());
            setupService.updateMessagePushState();
        }
    }

    /**
     * 漫画更新推送状态改变事件
     */
    public void comicUpdatePushBtnOnClick(View view) {
        SwitchButton switchButton = ObjectUtil.as(view, SwitchButton.class);
        if(ObjectUtil.nonNull(switchButton)) {
            DeviceConfigInfo.updateComicUpdatePushEnable(switchButton.isChecked());
            setupService.updateMessagePushState();
        }
    }

    /**
     * 漫画推荐推送状态改变事件
     */
    public void recommendPushBtnOnClick(View view) {
        SwitchButton switchButton = ObjectUtil.as(view, SwitchButton.class);
        if(ObjectUtil.nonNull(switchButton)) {
            DeviceConfigInfo.updateRecommendPushEnable(switchButton.isChecked());
            setupService.updateMessagePushState();
        }
    }

    /**
     * 评论推送状态改变事件
     */
    public void commentPushBtnOnClick(View view) {
        SwitchButton switchButton = ObjectUtil.as(view, SwitchButton.class);
        if(ObjectUtil.nonNull(switchButton)) {
            DeviceConfigInfo.updateCommentPushEnable(switchButton.isChecked());
            setupService.updateMessagePushState();
        }
    }

}
